/*
 * Date            Author     Note
 * 2015.04.27      wzwz       机头前置量
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "mac_com.h"
#include "com.h"
#include "keypane.h"
#include "info.h"

/*
 *
 */
static void CreateMacComInterface(HWND hWnd)
{
    int i, j;
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_HELP_WND_BMP - CX_ASK_BMP - 15,
        8,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    //
    if(1)
    {
        CreateWindow (MH_LABEL_NAME,
            MW_HEAD_SPEED_CAPTION,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            13,
            50,
            120,
            26,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        char *str[10] = {
            "01: 01 - 05",
            "03: 11 - 15",
            "05: 21 - 25",
            "07: 31 - 35",
            "09: 41 - 45",
            "11: 51 - 55",
            "13: 61 - 65",
            "15: 71 - 75",
            "17: 81 - 85",
            "19: 91 - 95",
        };
        for(i = 0; i < 10; ++i)
        {

            CreateWindow (MH_LABEL_NAME,
                str[i],
                WS_CHILD | WS_VISIBLE,
                IDC_STATIC,
                13,
                50 + 26 * (i + 1),
                120,
                26,
                hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        }
    }
    if(1)
    {
        //
        CreateWindow (MH_LABEL_NAME,
            MW_LEFT_DRT_CAPTION,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            13 + 120,
            50,
            40,
            26,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        //
        CreateWindow (MH_LABEL_NAME,
            MW_RIGHT_DRT_CAPTION,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            173,
            50,
            40,
            26,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        char str[32] = {0};
        for(i = 0; i < 2; ++i)
        {
            for(j = 0; j < 10; ++j)
            {
                sprintf(str, "%d", GetMacBeforeCom(g_knit_data, i, j * 2));
                CreateWindow (MH_EDIT_NAME,
                    str,
                    WS_CHILD | BS_CENTER | WS_VISIBLE,
                    IDC_EDIT1 + i * 10 + j,
                    133 + 40 * i,
                    50 + 26 * (j + 1),
                    40,
                    26,
                    hWnd, 0);
            }
        }
    }
    if(1)
    {
        CreateWindow (MH_LABEL_NAME,
            MW_HEAD_SPEED_CAPTION,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            133 + 40 * 2,
            50,
            120,
            26,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        char *str[10] = {
            "02: 06 - 10",
            "04: 16 - 20",
            "06: 26 - 30",
            "08: 36 - 40",
            "10: 46 - 50",
            "12: 56 - 60",
            "14: 66 - 70",
            "16: 76 - 80",
            "18: 86 - 90",
            "20: 96 -100",
        };
        for(i = 0; i < 10; ++i)
        {

            CreateWindow (MH_LABEL_NAME,
                str[i],
                WS_CHILD | WS_VISIBLE,
                IDC_STATIC,
                133 + 40 * 2,
                50 + 26 * (i + 1),
                120,
                26,
                hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        }
    }
    if(1)
    {
        //
        CreateWindow (MH_LABEL_NAME,
            MW_LEFT_DRT_CAPTION,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            13 + 120 * 2 + 40 * 2,
            50,
            40,
            26,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        //
        CreateWindow (MH_LABEL_NAME,
            MW_RIGHT_DRT_CAPTION,
            WS_CHILD | WS_VISIBLE,
            IDC_STATIC,
            13 + 120 * 2 + 40 * 3,
            50,
            40,
            26,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
        char str[32] = {0};
        for(i = 0; i < 2; ++i)
        {
            for(j = 0; j < 10; ++j)
            {
                sprintf(str, "%d", GetMacBeforeCom(g_knit_data, i,  j * 2 + 1));
                CreateWindow (MH_EDIT_NAME,
                    str,
                    WS_CHILD | BS_CENTER | WS_VISIBLE,
                    IDC_EDIT1 + (i + 2) * 10 + j,
                    13 + 120 * 2 + 40 * (2 + i),
                    50 + 26 * (j + 1),
                    40,
                    26,
                    hWnd, 0);
            }
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    int n = atoi(sc);
    int drt = (((nID - IDC_EDIT1) / 10) & 1) ? 1 : 0;
    int sect = ((nID - IDC_EDIT1) % 10) * 2;
    sect = (nID - IDC_EDIT1) < 20 ? sect : sect + 1;
    SetMacBeforeCom(g_knit_data, drt, sect, n);
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 40)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        int value = atoi(GetWindowCaption(hTmp));
        CreateKeyboardWindow(hWnd, rect, 0, 100, value);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        if(GetWindowAdditionalData(hWnd))
        {
            int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
            if(ret)
            {
                printf("write yarn com: %d\n", ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                MH_FILE_SYNC();
            }
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        OnBtnCmd(hWnd, nID);
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 30)
        {
            nID += 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 10 && nID < IDC_EDIT1 + 40)
        {
            nID -= 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 40)
        {
            if((nID - IDC_EDIT1) % 10)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 40)
        {
            if(((nID - IDC_EDIT1) % 10) < 9)
            {
                ++nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
        }
    }
}
/*
 *
 */
static int MacComWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateMacComInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_CYAN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_HELP]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateMacComWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_HELP_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_HELP_WND_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(597);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MacComWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_HELP_WND_BMP;
    CreateInfo.by = y + CY_HELP_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
